import requests
from bs4 import BeautifulSoup
import re
allUniv = []

#获取指定页面的信息
def getHttpText(url):
    try:
        rsp = requests.get(url,timeout=30)
        rsp.raise_for_status()
        rsp.encoding = "utf-8"
        return rsp.text
    except:
        return "error"
#通过查找获取指定数据
def findText(text):
    soup = BeautifulSoup(text, "html.parser")
    return soup.findAll(string=re.compile("百度"))

#通过匹配标签来获取相关信息
def fillList(text):
    soup = BeautifulSoup(text, "html.parser")
    data = soup.find_all('tr')
    #print(data)

    for tr in data:
        ltd = tr.find_all('td')
        if len(ltd)==0:
            continue
        singleUniv = []
        for td in ltd:
            print("~~~"+str(td.string))
            la = td.find('a')
            print(la)
            if la is not None:
                if len(la) > 0:
                    print("----"+str(la.string))
                    singleUniv.append(la.string)
            else:
                td = str(td.string).replace("\n","").replace("None","").strip()
                print(td)
                if td!='':
                    print("===="+str(len(td)))
                    singleUniv.append(td)
            #singleUniv.append(td.string)
        print("每行信息："+str(singleUniv))
        allUniv.append(singleUniv)

def printUnivlist(num):
    print("{:^4}{:^10}{:^8}{:^8}".format("排名","校名",'总分','层次'))
    for i in range(num):
        u=allUniv[i]
        print("{:^4}{:^10}{:^8}{:^8}".format(u[0],u[1],u[2],u[3]))